'use strict';

const {
	Controller
} = require('uni-cloud-router');

module.exports = class WxserviceController extends Controller {

	// 微信JSSDK签名
	async getJssdkSignature() {
		const response = {
			code: 1,
			datas: {}
		}
		
		const {
			url
		} = this.ctx.data;
		
		// 公众号的唯一标识
		response.datas.appId = this.service.h5wx.appid;
		// 生成签名的时间戳
		response.datas.timestamp = parseInt(Date.now() / 1000);
		// 生成签名的随机串
		response.datas.nonceStr = Math.random().toString(36).substr(2, 15);
		// 签名
		response.datas.signature = await this.service.h5wx.get_jssdk_signature({
			timestamp: response.datas.timestamp,
			noncestr: response.datas.nonceStr,
			url: url
		});
		response.code = 0;
		
		return response;
	}
	
}
